<?php
class AdminController extends EController
{
	public $layout = 'main';
	//过滤器
	public function filters()
	{
		return array(
			array(
				'application.filters.BackEndAuthRequestFilter',
			)
		);
	}

	public function getAllRole()
	{
		$roleArr =  Role::model()->findAll();
		$arr=array();
		foreach ($roleArr as $r){
           $arr[$r["role_id"]]=$r["role_name"];
  		}
		return $arr;
	}	

	//管理员管理
	public function actionAdminManage()
	{
		$this->layout='list';
		$this->son_menu=0;
		$act_list=ActionMenuHelper::getHiddenMenu();
		$model=new Admin('search');
		if(isset($_GET['Admin']))
			$model->attributes=$_GET['Admin'];
		$this->render('list',array(
			'dataProvider'=>$model->search(),
			'act_list'=>$act_list,
				'model'=>$model
			)
		);
	}

	//添加管理员
	public function actionAddAdmin()
	{
		$this->layout='list';
		$this->son_menu=1;
		$model = new AdminAddForm;
		//ajax 验证
		if (isset($_POST['ajax']) && $_POST['ajax'] === 'ajax_form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}	

		if(isset($_POST['AdminAddForm']))
		{
			$model->attributes=$_POST['AdminAddForm'];
			// 验证用户输入，并在判断输入正确后重定向到
			if($model->validate())
			{
				if($model->addAdmin())
				{
					$this->showMessage(Yii::t('info','operation success'),'admin/admin/adminmanage');
				}	
				else
				{
					$this->showMessage(Yii::t('info','operation failed'),'admin/admin/adminmanage');
				}
			}
		}
		$this->render('add',array('model'=>$model));
	}

	//编辑管理员
	public function actionEditAdmin()
	{
		$this->layout='pop';
		$admin_id = $this->_getAdminId();
		$admin = Admin::model()->findByPk($admin_id);
		$model = new AdminEditForm;
		$model->attributes = $admin->attributes;
		$model->user_pwd='';
		//ajax 验证
		if (isset($_POST['ajax']) && $_POST['ajax'] === 'ajax_form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		if(isset($_POST['AdminEditForm']))
		{
			$model->attributes=$_POST['AdminEditForm'];
			// 验证用户输入，并在判断输入正确后重定向到
			if($model->validate())
			{
				if($model->editAdmin($admin_id))
				{
					Yii::app()->user->setFlash('success',Yii::t('info','operation success'));
				}
				else
				{
					Yii::app()->user->setFlash('failed',Yii::t('info','operation failed'));
				}
				$this->refresh();
			}
		}
		$this->render('edit',array('model'=>$model,'admin'=>$admin));
	}

	//删除管理员
	public function actionDeleteAdmin()
	{
		$admin_id = $this->_getAdminId();
		if(Admin::model()->deleteByPk($admin_id))
		{
			$this->showMessage(Yii::t('info','operation success'));
		}
		else
		{
			$this->showMessage(Yii::t('info','operation failed'));
		}
	}

	//获取id
	private function _getAdminId(){
		if(!isset($_GET['admin_id']) || !is_numeric($_GET['admin_id'])){
			throw new CHttpException(400,'Invalid request. admin_id is required!');
		}
		return $_GET['admin_id'];
	}

//	public function actionccccc()
//    {
//        $date1 = mktime(0,0,0,12,10,2019);
//        $date2 = mktime(0,0,0,10,25,2019);
//        $day = round(($date1-$date2)/3600/24);
//        for($i = 1;$i < $day;$i++ ){
//            $addtime = date("Ymd",strtotime("-".$i." day"));
//            $table = "click_log_".$addtime;
//            $connection = Yii::app()->tongji; //连接
//            $all_sql = "insert into tongji_log (sourceid,plat,type,classid,classpid,ip,pv,bd,author,addtime) SELECT sourceid,plat,type,classid,classpid,count(DISTINCT(ip)) AS userip,count(*) AS pv,sum(bd),author,'".$addtime."' FROM ".$table." GROUP BY sourceid,type";
//            $command = $connection->createCommand($all_sql);
//            if ($result = $command->execute()){
//                echo 'ok';
//            }
//        }
//    }
}